package com.baozhun.mall.common.util

import android.graphics.Canvas
import android.graphics.Paint
import android.text.TextPaint
import android.text.style.ReplacementSpan
import com.blankj.utilcode.util.SizeUtils
import com.orhanobut.logger.Logger


/**
 * @Title：
 * @Description：
 * @Author：xuefeng.zhu
 * @Since：2023/12/28
 * @Version：1.0.0
 */
class VerticalBottomSpan(//px
    private val fontSizePx: Int, //px
    private var mOffset: Float
) : ReplacementSpan() {
    init {
        mOffset = mOffset
    }

    override fun getSize(paint: Paint, text: CharSequence?, start: Int, end: Int, fm: Paint.FontMetricsInt?): Int {
        var text = text
        text = text?.subSequence(start, end)
        val p: Paint = getCustomTextPaint(paint)
        return p.measureText(text.toString()).toInt()
    }

    override fun draw(canvas: Canvas, text: CharSequence, start: Int, end: Int, x: Float, top: Int, y: Int, bottom: Int, paint: Paint) {
        var text = text
        text = text.subSequence(start, end)
        val p: Paint = getCustomTextPaint(paint)
        val fm = p.fontMetricsInt
        // 此处重新计算y坐标，使字体居中
        canvas.drawText(text.toString(), x, y + mOffset, p)
        Logger.w(
            "VerticalCenterSpan", "..start.." + start + "....end...." + end + "....x....." + x + "...y..." + y +
                    "...bottom..." + bottom + "...top.." + top
        )
    }

    private fun getCustomTextPaint(srcPaint: Paint): TextPaint {
        val paint = TextPaint(srcPaint)
        paint.textSize = SizeUtils.dp2px(fontSizePx.toFloat()).toFloat() //设定字体大小, sp转换为px
        return paint
    }
}